const express = require('express');
const axios = require('axios');
const router = express.Router();

// const app = express();
// const PORT = 3005;

router.get('/', async (req, res) => {

    try {
        let { trainDate, fromStation, toStation, trainNo } = req.query;

        const { data: result } = await axios.get('https://kyfw.12306.cn/otn/czxx/queryByTrainNo', {
            params: {
                train_no: trainNo,
                from_station_telecode: fromStation,
                to_station_telecode: toStation,
                depart_date: trainDate,
            },
            headers: {
                // 请求查询接口成功关键语句
                /**
                 * 设置cookie为以下配置中的任何一个即可请求成功，12306官网为全部配置：
                 * _jc_save_fromStation=%u4E0A%u6D77%2CSHH; _jc_save_toStation=%u5929%u6D25%2CTJP; _jc_save_fromDate=2023-06-03; _jc_save_toDate=2023-06-03; _jc_save_wfdc_flag=dc
                 */
                Cookie: 'JSESSIONID='
            }
        });


        let list = []
        const { data: { data }, httpstatus, status } = result
        if (httpstatus === 200 || status === true) {
            list = data
        }

        console.error('22222222', list, httpstatus)

        // 返回查询结果
        res.json(list)

    } catch (error) {
        res.json([])
    }
});

module.exports = router
